<?php

class Taichinh_model extends CI_Model
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
		date_default_timezone_set('Asia/Ho_Chi_Minh');
	}
	
	// Public methods
	public function get_all_chung_tu($limit,$start)
	{
		$this->db->select('f_chung_tu.*, f_hang_muc.name as name, nguon.name as nguon');
		$this->db->from('f_chung_tu');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_chung_tu.hang_muc_id', 'left');
		$this->db->join('f_hang_muc as nguon', 'nguon.id = f_chung_tu.nguon_id', 'left');
		$this->db->order_by('f_chung_tu.id','desc');
		$query = $this->db->get();
		return $query->result_array();;
	}
	
	public function get_all_hang_muc($limit,$start)
	{
		$this->db->select('*');
		$this->db->from('f_hang_muc');
		$this->db->order_by('id','desc');
		$query = $this->db->get();
		return $query->result_array();;
	}
	
	// Lây danh sách do du dau ky nhap
	function get_all_sddk()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->where('f_tong_hop.kind', 1);
		$query = $this->db->get();
		return $query->result_array();
	}
	
	//////////////////////// END RP TONG HOP ///////////////
	function get_report_tong_hop($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nam(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nua(); break;
			default :
				return $this->get_report_tong_hop_thang(); break;
		}
	
	}
	
	function get_report_tong_hop_thang()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 1);
		$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(f_tong_hop.created_date)',date('m'));
		$query = $this->db->get();
	
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	function get_report_tong_hop_nam()
	{
		return null;
	}
	function get_report_tong_hop_quy()
	{
		return null;
	}
	function get_report_tong_hop_nua()
	{
		return null;
	}
	
	// Report tong hop theo nguon tien {ngan sach, hoc phi, cac hang muc chi}
	function get_report_tong_hop_nguon($duration=null)
	{
		if($duration == null) $duration = 'THISMONTH';
		switch ($duration)
		{
			case 'THISMONTH':
				return $this->get_report_tong_hop_nguon_thang(); break;
			case 'THISYEAR':
				return $this->get_report_tong_hop_nguon_nam(); break;
			case 'QUATER':
				return $this->get_report_tong_hop_nguon_quy(); break;
			case 'HALF':
				return $this->get_report_tong_hop_nguon_nua(); break;
			default :
				return $this->get_report_tong_hop_nguon_thang(); break;
		}
	
	}
	
	function get_report_tong_hop_nguon_thang()
	{
		$this->db->select('f_tong_hop.*, f_hang_muc.name as item');
		$this->db->from('f_tong_hop');
		$this->db->join('f_hang_muc', 'f_hang_muc.id = f_tong_hop.hang_muc_id', 'left');
		$this->db->order_by('f_tong_hop.id','desc');
		$this->db->where('f_tong_hop.kind', 0);
		$this->db->where('f_hang_muc.kind', 0);// ngoai he thong
		$this->db->where('YEAR(f_tong_hop.created_date)',date('Y'));
		$this->db->where('MONTH(f_tong_hop.created_date)',date('m'));
		$query = $this->db->get();
	
		$rss = $query->result_array();
		$ret = array();
		foreach($rss as $rs)
		{
			$sum = $this->tinh_tong_ps_theo_hang_muc_nguon_thang($rs['hang_muc_id']);
			if($sum)
			{
				if($sum[0]['tang'] == null) $sum[0]['tang'] = 0;
				if($sum[0]['giam'] == null) $sum[0]['giam'] = 0;
				$rs['ps_tang'] = $sum[0]['tang'];
				$rs['ps_giam'] = $sum[0]['giam'];
			}
			$ret[] = $rs;
		}
		return $ret;
	}
	
	public function count_all_chung_tu()
	{
		$this->db->select('id');
		$this->db->from('f_chung_tu');
		return $this->db->count_all_results();
	}
	
	public function count_all_hang_muc()
	{
		$this->db->select('id');
		$this->db->from('f_hang_muc');
		return $this->db->count_all_results();
	}
	
	public function change_status_article($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('site_content',$data);		
	}
	
}

/* End of file posts_model.php */
/* Location: ./application/modules/admin/models/posts_model.php */